package com.yfh.study.manager.controller;

import com.yfh.data.base.r.Codes;
import com.yfh.data.base.r.R;
import com.yfh.data.base.r.Rs;
import com.yfh.data.base.utils.QfBeanUtils;
import com.yfh.data.system.dataManager.vo.input.ClassificationAddInput;
import com.yfh.data.system.dataManager.vo.input.ClassificationDeleteInput;
import com.yfh.data.system.dataManager.vo.output.ClassificationTreeOutput;
import com.yfh.data.system.entity.Classification;
import com.yfh.study.manager.service.ClassificationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/classification")
@Slf4j
/**
 * 分类控制器
 */
public class ClassificationController {
    @Autowired
    private ClassificationService classificationService;

    @RequestMapping("/queryAllClassification")
    public R  getClassificationList(){
        log.info("[查询所有分类]");
        List<Classification> result = classificationService.getClassificationList();
        if (result == null){
            log.info("[查询所有分类]--失败");
            return Rs.createFail(Codes.FAIL);
        }
        //数据处理成为一个树状 offer 提供 poll 生产
        Map<Integer,ClassificationTreeOutput> node1 = new HashMap<>();
        Queue<ClassificationTreeOutput> node2 = new LinkedList();
        Queue<ClassificationTreeOutput> node3 = new LinkedList();

        Queue<ClassificationTreeOutput> resultTree = new LinkedList();

        for (Classification classification : result) {
            if (classification.getType()==1) {
                ClassificationTreeOutput e = QfBeanUtils.beanCopy(classification, ClassificationTreeOutput.class);
                node1.put(classification.getId(),e);
                resultTree.add(e);
                continue;
            }
            if (classification.getType()==2) {
                ClassificationTreeOutput e = QfBeanUtils.beanCopy(classification, ClassificationTreeOutput.class);
                node2.offer(e);
                continue;
            }
            if (classification.getType()==3) {
                ClassificationTreeOutput e = QfBeanUtils.beanCopy(classification, ClassificationTreeOutput.class);
                node3.offer(e);
                continue;
            }
        }

        while (!node2.isEmpty()) {
            ClassificationTreeOutput poll = node2.poll();
            Integer pid = poll.getPid();
            //加入第一层
            node1.get(pid).getNode().add(poll);
            //提供给下一层
            node1.put(poll.getId(),poll);
        }
        while (!node3.isEmpty()) {
            ClassificationTreeOutput poll = node3.poll();
            Integer pid = poll.getPid();
            //加入第一层
            node1.get(pid).getNode().add(poll);
        }

        log.info("[查询所有分类]--成功");
        return Rs.create(resultTree);
    }

    /**
     * 添加分类成功
     * @param classificationAddInput
     * @return
     */
    @RequestMapping("/insertClassification")
    public R insertClassification(ClassificationAddInput classificationAddInput){
        log.info("[添加分类]");
        boolean result =classificationService.insertClassification(classificationAddInput);
        if (!result) {
            log.info("[添加分类]--失败");
            return Rs.createFail(Codes.FAIL);
        }
        log.info("[添加分类]--成功");
        return Rs.create("SUCC");
    }
    /**
     * 删除分类
     * @param classificationDeleteInput
     * @return
     */
    @RequestMapping("/deleteClassification")
    public R deleteClassification(ClassificationDeleteInput classificationDeleteInput){
        log.info("[删除分类]");
        //确定下一级没有分类
        Integer count = classificationService.queryByPid(classificationDeleteInput.getId(),classificationDeleteInput.getType());
        if (count!=0) {
            log.info("[删除分类] -子级存在"+count+"个没被删除");
            return Rs.createFail(Codes.FAIL,"子级存在"+count+"个没被删除");
        }
        //删除
        boolean result = classificationService.deleteByid(classificationDeleteInput.getId());
        if (!result) {
            log.info("[删除分类] -删除失败");
            return Rs.createFail(Codes.FAIL);
        }

        log.info("[删除分类] 成功 -{}",classificationDeleteInput);
        return Rs.create("SUCC");
    }


}
